<template>
  <div class="home" id="meun">
    <el-container>
      <el-header class="headBody" v-if="userInfo.userType == 1">
        <div class="headBody_left">
          <div class="logo">
            <img src="@/assets/img/logo1.png" alt="" />
          </div>
          <!-- <img src="@/assets/img/logo1.png" alt="" /> -->
          <div>试验机联网动态监控系统</div>
        </div>
        <div class="headBody_right">
          <el-button class="head_text" type="text" icon="el-icon-s-home"
            >APP</el-button
          >
          <el-button
            class="head_text"
            type="text"
            icon="el-icon-s-home"
            @click="goHome"
            >首页</el-button
          >
          <el-button
            class="head_text"
            type="text"
            icon="el-icon-d-arrow-right"
            @click="back()"
            >退出</el-button
          >
          <el-button
            class="head_text"
            type="text"
            icon="el-icon-rank"
            @click="screen"
            >全屏</el-button
          >
        </div>
      </el-header>
      <el-main>
        <el-container style="height: 100%">
          <el-aside :width="isCollapse ? '50px' : '200px'">
            <i
              class="el-icon-s-fold toggle-button"
              @click="toggleCollapse"
              v-if="isCollapse == false"
            ></i>
            <i
              class="el-icon-s-unfold toggle-button"
              @click="toggleCollapse"
              v-else
            ></i>
            <!-- 菜单 -->
     

            <el-menu
              ref="menu"
              :default-active="activePath"
              router
              unique-opened
              :collapse-transition="false"
              :collapse="isCollapse"
              class="sidebar el-menu-vertical-demo"
              @select="openMenu"
              active-text-color="rgb(51, 85, 199)"
              text-color="#fff"
            >
              <menu-tree :menuData="leftNameData"></menu-tree>
            </el-menu>

            <div class="userInfo">
              <div class="iconsv">
                <img
                  class="iconimg"
                  :src="userInfo.gender == 0 ? girls : boys"
                  alt=""
                />
                <!-- <img src="../assets/img/organization/boys.png" alt=""> -->
              </div>
              <div class="name">{{ userInfo.realName }}</div>
            </div>
          </el-aside>

          <el-container>
            <div class="content">
              <router-view v-if="isReloadAlive" />
            </div>
          </el-container>
        </el-container>
      </el-main>
    </el-container>
  </div>
</template>

<script>
import menuTree from "@/components/commonPublic/menu.vue";
import {
  getBaseModuleList, //功能目录列表
} from "@/api/request.js";
export default {
  name: "Home",
  components: {
    menuTree,
  },
  provide() {
    return {
      reload: this.reload,
    };
  },

  data() {
    return {
      isReloadAlive: true,
      // 是否全屏
      fullscreen: false,
      // 被激活的链接地址
      activePath: "/index",
      // 是否折叠
      isCollapse: false,
      fristModule: [],
      userInfo: {},
      boys: require("../assets/img/organization/boys.png"),
      girls: require("../assets/img/organization/girls.png"),
      leftNameData: this.$store.state.leftNameData,
    };
  },
  created() {
    this.userInfo = JSON.parse(sessionStorage.getItem("monitorBack-userInfo"));
  },
  // computed: {
  //   getOutterNum: function () {
  //     console.log(this.$store.state.leftNameData);
  //     return this.$store.state.leftNameData
  //   }
  // },
  mounted() {
    this.activePath = sessionStorage.getItem("monitorBack-keyPath") || "/index";
    this.roleModule();
  },
  watch: {
    activePath(newValue) {
      if (newValue != this.$route.path) {
        this.activePath = this.$route.path;
      }
    },
  },

  methods: {
    goHome() {
      this.$router.push({ path: "/index" });
    },
    roleModule() {
      var ModuleList = JSON.parse(sessionStorage.getItem("monitorBack-ModuleList"));
      var fristModule = [];
      if (ModuleList.length != 0) {
        this.leftNameData.map((i) => {
          //筛选大模块
          ModuleList.map((item) => {
            if (item.fEncode == i.name) {
              i.title = item.fFullname;
              fristModule.push(i);
            }
          });
        });

        fristModule.map((item) => {
          //只有两级
          item["childrens"] = [];
          item.children.map((i) => {
            ModuleList.map((j) => {
              if (i.name == j.fEncode) {
                // console.log(i, j);
                i.title = j.fFullname;
                item["childrens"].push(i);
              }
            });
          });
          item.children = item.childrens;
        });
        // 有三级
        fristModule.map((item) => {
          item.children.map((i) => {
            //二级
            i["childrens"] = [];
            i.children.map((h) => {
              //循环3级
              ModuleList.map((j) => {
                if (h.name == j.fEncode) {
                  h.title = j.fFullname;
                  i["childrens"].push(h);
                }
              });
            });

            i.children = i.childrens;
          });
        });

        fristModule.map((item) => {
          item.children.map((i) => {
            if (i.rank == 2) {
              i.path2 = "";
              console.log("BAOCUO");
              ModuleList.map((j) => {
                if (i.name2 == j.fEncode) {
                  i.path2 = i.path + "Text";
                }
              });
            }
          });
        });

        this.leftNameData = fristModule;
      } else if (
        JSON.parse(sessionStorage.getItem("monitorBack-userInfo")).enCode == "System"
      ) {
        this.getTree();
      } else {
        this.leftNameData = [];
        return this.$message.error("请配置对应权限");
      }
    },

    leftNameDataData(val, data) {
      // val:树接口  data:本地
      let list = [];
      if (!Array.isArray(data)) {
        return list;
      }
      list = data.map((item) => {
        if (item.children.length > 0) {
          this.leftNameDataData(val, item.children);
        }
        val.map((i) => {
          // 接口等于本地
          if (i.fEncode == item.name) {
            item.title = i.fFullname;
          }
        });

        return item;
      });

      return list;
    },
    getTree() {
      getBaseModuleList({
        fModuleid: "",
      })
        .then((res) => {
          if (res.errcode == 0) {
            var companyTree = res.data.baseModuleList;
            this.leftNameData = this.leftNameDataData(
              companyTree,
              this.leftNameData
            );
          } else {
            this.$message.error(res.msg);
            return false;
          }
        })
        .catch(() => {
          return false;
        });
    },
    reload() {
      this.isReloadAlive = false;
      this.$nextTick(function () {
        this.isReloadAlive = true;
      });
    },

    openMenu(keyPath) {
      if (keyPath != "null") {
        sessionStorage.setItem("monitorBack-keyPath", keyPath);
      }
    },
    // 是否全屏
    screen() {
      let element = document.documentElement;
      if (this.fullscreen) {
        if (document.exitFullscreen) {
          document.exitFullscreen();
        } else if (document.webkitCancelFullScreen) {
          document.webkitCancelFullScreen();
        } else if (document.mozCancelFullScreen) {
          document.mozCancelFullScreen();
        } else if (document.msExitFullscreen) {
          document.msExitFullscreen();
        }
      } else {
        if (element.requestFullscreen) {
          element.requestFullscreen();
        } else if (element.webkitRequestFullScreen) {
          element.webkitRequestFullScreen();
        } else if (element.mozRequestFullScreen) {
          element.mozRequestFullScreen();
        } else if (element.msRequestFullscreen) {
          // IE11
          element.msRequestFullscreen();
        }
      }
      this.fullscreen = !this.fullscreen;
    },

    toggleCollapse() {
      this.isCollapse = !this.isCollapse;
    },

    back() {
      this.$confirm("是否继续退出?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          window.sessionStorage.removeItem("monitorBack-token");
          this.$router.push("/");
        })
        .catch(() => {
          this.loading = false;
          this.$message.info("取消退出");
        });
    },
  },
};
</script>
<style scoped lang="less">
a {
  color: #fff;
}

.home {
  width: 100%;
  height: 100%;
}

.home_header {
  background-color: #1559ba;
  text-align: center;
  height: 10%;
  width: 100%;
}

/* 右侧 */
.main_right {
  width: 100%;
  height: 100%;
  padding: 0 15px;
  box-sizing: border-box;
  overflow: hidden;
}

.main_right_position {
  text-align: left;
  width: 100%;
}

.main_right_table {
  width: 100%;
  height: 90%;
}

.content {
  width: 100%;
  height: 100%;
  padding: 0 5px;
  box-sizing: border-box;
}

.toggle-button {
  display: block;
  background-color: #1559ba;
  font-size: 17px;
  line-height: 30px;
  color: #fff;
  text-align: center;
  letter-spacing: 0.2em;
  cursor: pointer;
}

.el-menu-vertical-demo:not(.el-menu--collapse) {
  background: #0c204d !important;
}

.el-aside {
  position: relative;
}

.userInfo {
  position: absolute;
  width: 100%;
  height: 50px;
  bottom: 0;
  display: flex;
  align-items: center;
  border-top: 1px solid #63656b;
  padding: 0 35px 0 25px;
  box-sizing: border-box;
  background: #0c204d;

  .iconsv {
    width: 36px;
    height: 36px;
    border-radius: 50%;
    line-height: 0px;
    overflow: hidden;
    margin-right: 15px;

    .iconimg {
      width: 100%;
      height: 100%;
    }
  }

  .name {
    height: 100%;
    line-height: 49px;

    font-size: 12px;
    color: #a6acbc;
  }
}

#meun /deep/.el-menu-vertical-demo {
  height: calc(100% - 82px) !important;
  overflow-y: auto;
}

.secondaryView {
  display: flex;
  align-items: center;
}
.headBody_left {
  padding-left: 0.2rem;
}
.logo {
  width: 0.26rem;
  height: 0.26rem;
  display: flex;
  justify-content: center;
  align-items: center;

  img {
    width: 100%;
    height: 100%;
  }
}
</style>
